package nl.mayisal.startup;

import java.sql.SQLException;

import nl.mayisal.metadata.TableStructure;
import nl.mayisal.sql.SQLConnection;
import nl.mayisal.sql.SQLConnector;
import nl.mayisal.sql.SQLResultSet;

public class TableAnalyser implements Runnable {
	private final TableStructure table;

	public TableAnalyser(TableStructure table) {
		this.table = table;
	}

	@Override
	public void run() {
		try {
			SQLConnection connection = new SQLConnector().getConnection();
			SQLResultSet rs = connection.query("SELECT SUM(1) AS count FROM "+this.table.getName());
			if (rs.next()) {
				int count = rs.getInt(1);
				this.table.setRecordCount(count);
			}
			rs.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

}
